<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
                 label-width="68px">
            <el-form-item label="用户姓名" prop="userId">
                <el-input v-model="queryParams.userName" placeholder="请输入用户姓名" clearable
                          @keyup.enter.native="handleQuery"/>
            </el-form-item>
            <el-form-item label="考站" prop="placeId">
                <el-select v-model="queryParams.placeId" placeholder="请选择考站" clearable>
                    <el-option
                        v-for="item in ex_placeList"
                        :key="item.key"
                        :label="item.label"
                        :value="item.key">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="是否永久" prop="forever">
                <el-radio-group v-model="queryParams.forever" size="mini">
                    <el-radio-button label="1">永久</el-radio-button>
                    <el-radio-button label="2">非永久</el-radio-button>
                </el-radio-group>
            </el-form-item>
<!--            <el-form-item label="开始时间" prop="startTime">
                <el-date-picker clearable v-model="queryParams.startTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择开始时间"></el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
                <el-date-picker clearable v-model="queryParams.endTime" type="date" value-format="yyyy-MM-dd"
                                placeholder="请选择结束时间"></el-date-picker>
            </el-form-item>-->
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                           v-hasPermi="['face:face_white:add']">新增
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                           @click="handleDelete" v-hasPermi="['face:face_white:remove']">删除
                </el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                           v-hasPermi="['face:face_white:export']">导出
                </el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="face_whiteList" @selection-change="handleSelectionChange" :height="table_height">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="用户" align="center" prop="userName"/>
            <el-table-column label="考站" align="center" prop="placeName"/>
            <el-table-column label="是否永久" align="center" prop="forever">
                <template slot-scope="scope">
                    <el-tag type="success" effect="plain" v-if="scope.row.forever == '1'">永久</el-tag>
                    <el-tag type="danger" effect="plain" v-else>非永久</el-tag>
                </template>
            </el-table-column>
            <el-table-column label="开始时间" align="center" prop="startTime" width="240">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.startTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="结束时间" align="center" prop="endTime" width="240">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.endTime) }}</span>
                </template>
            </el-table-column>
            <el-table-column label="备注" align="center" prop="remark"/>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                               v-hasPermi="['face:face_white:edit']">修改
                    </el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                               v-hasPermi="['face:face_white:remove']">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                    @pagination="getList"/>

        <!-- 添加或修改人脸白名单对话框 -->
        <el-dialog :title="title" :visible.sync="open" :width="(is_add ? 1300 : 600) +'px'" append-to-body>
            <el-form v-if="is_add" ref="form" :model="form" :rules="rules" label-width="80px" label-position="top">
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="归属科室(筛序学生使用，不参与白名单参数)" prop="deptId">
                            <treeselect v-model="form.deptId" :options="deptOptions" :show-count="true"
                                        placeholder="请选择归属科室" style="width: 220px"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="选择用户" prop="userList">
                            <el-transfer :titles="['已选择', '未选择']" filterable :filter-method="userFilterMethod"
                                         filter-placeholder="请输入用户姓名" v-model="form.userList"
                                         :data="user_list"></el-transfer>
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="选择考站" prop="placeList">
                            <el-transfer :titles="['已选择', '未选择']" filterable :filter-method="placeFilterMethod"
                                         filter-placeholder="请输入考站名称" v-model="form.placeList"
                                         :data="ex_placeList"/>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="是否永久" prop="forever">
                                    <el-switch v-model="form.forever" active-color="#13ce66" inactive-color="#ff4949"
                                               active-value="1" active-text="永久" inactive-value="2"
                                               inactive-text="非永久"></el-switch>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12" v-if="form.forever == '2'">
                                <el-form-item label="时间范围" prop="timeRange">
                                    <el-date-picker v-model="form.timeRange" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss"
                                                    :picker-options="pickerOptions" range-separator="~"
                                                    start-placeholder="开始日期" end-placeholder="结束日期"
                                                    align="center"/>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="备注" prop="remark">
                                    <el-input type="textarea" :rows="2" v-model="form.remark" placeholder="请输入备注"/>
                                </el-form-item>
                            </el-col>
                        </el-row>

                    </el-col>
                </el-row>
            </el-form>

            <el-form v-else ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="用户" prop="userId">
                    <el-tag effect="plain">{{form.userName}}</el-tag>
                </el-form-item>
                <el-form-item label="考站编号" prop="placeId">
                    <el-select v-model="form.placeId" placeholder="请选择考站" clearable>
                        <el-option v-for="item in ex_placeList" :key="item.key" :label="item.label" :value="item.key"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否永久" prop="forever">
                    <el-switch v-model="form.forever" active-color="#13ce66" inactive-color="#ff4949"
                               active-value="1" active-text="永久" inactive-value="2"
                               inactive-text="非永久"></el-switch>
                </el-form-item>
                <el-form-item label="时间范围" prop="timeRange" v-if="form.forever == '2'">
                    <el-date-picker v-model="form.timeRange" type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss"
                                    :picker-options="pickerOptions" range-separator="~" style="width: 360px"
                                    start-placeholder="开始日期" end-placeholder="结束日期"
                                    align="center"/>
                </el-form-item>
                <el-form-item label="备注" prop="remark">
                    <el-input type="textarea" :rows="2" v-model="form.remark" style="width: 360px" placeholder="请输入备注"/>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {
    listFace_white,
    delFace_white,
    updateFace_white,
    addFace_white_more
} from "@/api/face/face_white";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {deptTreeSelect, listUser} from "@/api/system/user";
import {listEx_place} from "@/api/basic/ex_place";
import {nowDateStr} from "@/utils/zzDouUtils/dateUtil";

export default {
    name: "Face_white",
    components: {Treeselect},
    data() {
        return {
            // 科室账号
            deptName: undefined,
            // 科室树选项
            deptOptions: undefined,
            defaultProps: {
                children: "children",
                label: "label"
            },
            // 遮罩层
            loading: true,
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 人脸白名单表格数据
            face_whiteList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            is_add: true,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userId: null,
                placeId: null,
                forever: null,
                startTime: null,
                endTime: null,
                reservedOne: null,
                reservedTwo: null,
            },
            // 表单参数
            form: {},
            //用户表格
            user_list: [],
            // 考站表表格数据
            ex_placeList: [],
            // 表单校验
            rules: {
                placeId:[
                    {required: true, message: "请选择场地", trigger: "blur"},
                ],
                userList:[
                    {type: 'array', required: true, message: "请选择用户", trigger: "blur"}
                ],
                placeList:[
                    {type: 'array', required: true, message: "请选择场地", trigger: "blur"}
                ],
                timeRange: [
                    {type: 'array', required: true, message: "非永久状态下请选择时间", trigger: "blur"}
                ]
            },
            pickerOptions: {
                shortcuts: [{
                    text: '最近一周',
                    onClick(picker) {const end = new Date();const start = new Date();end.setTime(start.getTime() + 3600 * 1000 * 24 * 7);picker.$emit('pick', [start, end]);}
                }, {
                    text: '最近一个月',
                    onClick(picker) {const end = new Date();const start = new Date();end.setTime(start.getTime() + 3600 * 1000 * 24 * 30);picker.$emit('pick', [start, end]);}
                }, {
                    text: '最近三个月',
                    onClick(picker) {const end = new Date();const start = new Date();end.setTime(start.getTime() + 3600 * 1000 * 24 * 90);picker.$emit('pick', [start, end]);}
                }]
            }
        };
    },
    created() {
        this.getList();
        this.getDeptTree();
        this.getPlaceList();
    },
    watch: {
        'form.deptId'(n, o) {
            this.deptChange(n);
        },
    },
    methods: {
        /** 查询考站表列表 */
        getPlaceList() {
            listEx_place().then(response => {
                this.ex_placeList = response.rows.map(place=>{
                    return {key: place.id, label: place.name};
                })
            });
        },
        /** 查询科室下拉树结构 */
        getDeptTree() {
            deptTreeSelect().then(response => {
                this.deptOptions = response.data;
            });
        },
        /** 查询人脸白名单列表 */
        getList() {
            this.loading = true;
            listFace_white(this.queryParams).then(response => {
                this.face_whiteList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = this.is_add ? {
                deptId: null,
                remark: null,
                forever: '1',
                timeRange: [],
                userList: [],
                placeList: [],
            } : {};
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.is_add = true;
            this.reset(true);
            this.getUserList();
            this.open = true;
            this.title = "添加人脸白名单";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.is_add = false;
            this.reset();
            this.form = row;
            this.form.forever = row.forever + '';
            if (row.forever == '2'){
                this.form.timeRange = [row.startTime,row.endTime]
            }
            this.title = "修改人脸白名单";
            this.open = true;
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.forever == '2'){
                        this.form.startTime = this.form.timeRange[0];
                        this.form.endTime = this.form.timeRange[1];
                    }
                    if (this.form.id != null) {
                        updateFace_white(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addFace_white_more(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除人脸白名单编号为"' + ids + '"的数据项？').then(function () {
                return delFace_white(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => {
            });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('face/face_white/export', {
                ...this.queryParams
            }, `face_white_${nowDateStr()}.xlsx`)
        },
        // 选择科室事件
        deptChange(e) {
            this.getUserList(e);
        },
        /** 查询用户列表 */
        getUserList(deptId) {
            listUser({deptId: deptId}).then(response => {
                    this.user_list = response.rows.map(user => {
                        return {key: user.userId, label: user.nickName};
                    });
                }
            );
        },
        userFilterMethod(query, item) {
            return item.label.indexOf(query) > -1;
        },
        placeFilterMethod(query, item) {
            return item.label.indexOf(query) > -1;
        }
    }
};
</script>
<style>
.el-transfer-panel__body {
    height: 400px;
}

.el-transfer-panel__list {
    height: 400px;
}

.el-transfer-panel__list.is-filterable {
    height: 400px;
}
</style>
